﻿package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.DepotList;
import com.edward.model.User;


public class DepotListManager extends HibernateDaoSupport {

	public void addDepotList(DepotList depotList) {
		
		getHibernateTemplate().save(depotList);
	}
	
	public DepotList getDepotList(int id){
		DepotList d =(DepotList)getHibernateTemplate().get(DepotList.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<DepotList> listDepotList() {
		return getHibernateTemplate().find("from DepotList");
	}
	
	@SuppressWarnings("unchecked")
	public List<DepotList> listDepotList(String flag,boolean oversea) {
		if(oversea){
			return getHibernateTemplate().find("from DepotList d where d.flag ='" + flag +"' and d.warehouse in ('E','F','G')");
		}else{
			return getHibernateTemplate().find("from DepotList d where d.flag ='" + flag +"' and d.warehouse in ('A','B','C')");
		}
	}

	
	
	public void updateDepotList(DepotList depotList) {
	
		getHibernateTemplate().update(depotList);
	}
	
	public void deleteDepotList(int id){
		
		getHibernateTemplate().delete(getDepotList(id));
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String tab,boolean oversea) {
		List <DepotList>list;
		if(oversea){
			list = getHibernateTemplate().find("from DepotList d where d.flag = '" + tab +"' and d.warehouse in ('E','F','G')"  );
		}else{
			list = getHibernateTemplate().find("from DepotList d where d.flag = '" + tab +"' and d.warehouse in ('A','B','C')" );
		}
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <DepotList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String tab,boolean oversea,DepotList dl) {
		List <DepotList>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql;
		if(oversea){
			sql = "from DepotList d where d.flag = '" + tab +"' and d.warehouse in ('E','F','G') ";
		}else{
			sql = "from DepotList d where d.flag = '" + tab +"' and d.warehouse in ('A','B','C') " ;
		}
		
		if(!dl.getOrderNum().equals("")){
			sql += "and d.orderNum = '" + dl.getOrderNum() + "'";
		}
		if(!dl.getListNum().equals("")){
			sql += "and d.listNum = '" + dl.getListNum() + "'";
		}
		if(!dl.getDepartment().equals("")){
			sql += "and d.department = '" + dl.getDepartment() + "'";
		}
		if(!dl.getType().equals("")){
			sql += "and d.type = '" + dl.getType() + "'";
		}
		if(dl.getDate() != null && dl.getEndDate() == null ){
			String d = sdf.format(dl.getDate());
			sql += "and d.date >= '" + d + "'";
		}
		if(dl.getEndDate() != null && dl.getDate() == null ){
			String d = sdf.format(dl.getEndDate());
			sql += "and d.date <= '" + d + "'";
		}
		if(dl.getEndDate() != null && dl.getDate() != null ){
			String d1 = sdf.format(dl.getDate());
			String d2 = sdf.format(dl.getEndDate());
			sql += "and d.date between '" + d1 + "' and '" +d2  +"'";
		}
		list = getHibernateTemplate().find(sql);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <DepotList>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	//自动添加对应表格的单号
	@SuppressWarnings("unchecked")	
	public String listNum(String flag){
		List <DepotList>list = getHibernateTemplate().find("from DepotList d where d.flag = '" + flag +"'" );
		int i = list.size();
		if(i==0){
			return "000001";
		}else{
			String tmp = list.get(i-1).getListNum();
			while(tmp.indexOf(0) == '0'){
				tmp = tmp.substring(1);
			}
			int t = Integer.parseInt(tmp) + 1;
			tmp = t + "";
			for(int j=6;j>tmp.length();){
				tmp ="0"+tmp;
			}
			return tmp;
		}
		
		
	}
	
	@SuppressWarnings("unchecked")
	public String getUserName(String name){
		List <User>list = getHibernateTemplate().find("from User u where u.username = '" + name +"'" );
		return list.get(0).getDescn();
		
	}


}



